﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for PlatformAdapter
/// </summary>
public class PlatformAdapter
{
	public PlatformAdapter()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public List<PlatformEntity> getListPlatform()
    {
        BookContextDataContext bookContext = new BookContextDataContext();
        List<PlatformEntity> platforms = new List<PlatformEntity>();
        try
        {
            List<Platform> list = bookContext.Platforms.ToList();

            foreach (Platform platform in list)
            {
                platforms.Add(new PlatformEntity(platform));

            }
            return platforms;
        }
        catch
        {
            return null;
        }
    }
    public List<PlatformEntity> getListPlatform(long Categoryid)
    {
        BookContextDataContext bookContext = new BookContextDataContext();
        List<PlatformEntity> platforms = new List<PlatformEntity>();
        try
        {
            var list = from c in bookContext.CategoryDetails
                                  join f in bookContext.Platforms
                                  on c.PlatformID equals f.ID
                                  where c.CategoryID == Categoryid
                                  select f;

            foreach (Platform platform in list)
            {
                platforms.Add(new PlatformEntity(platform));
            }
            return platforms;
        }
        catch
        {
            return null;
        }
    }

    public List<PlatformEntity> GetPlatformByID(long id)
    {
        BookContextDataContext bookContext = new BookContextDataContext();
        List<PlatformEntity> platforms = new List<PlatformEntity>();
        try
        {
            Platform plat = bookContext.Platforms.Where(p => p.ID == id).FirstOrDefault();
            platforms.Add(new PlatformEntity(plat));
            return platforms;
        }
        catch
        {
            return null;
        }
    }

    /// <summary>
    /// Filter Platform by key word namePlatform
    /// </summary>
    public List<PlatformEntity> searchPlatform(string name)
    {
        List<PlatformEntity> platforms = new List<PlatformEntity>();
        BookContextDataContext bookContext = new BookContextDataContext();
        try
        {           
            var list = from p in bookContext.Platforms
                       where p.Name.Contains(name)
                       select p;
            foreach (Platform platform in list)
            {
                platforms.Add(new PlatformEntity(platform));
            }

            return platforms;
        }
        catch
        {
            return null;
        }
    }
    public long createPlatform(PlatformEntity newPlatform)
    {
        BookContextDataContext bookContext = new BookContextDataContext();
        try
        {
            Platform platform = new Platform();
            platform.Name = newPlatform.Name;
            platform.Description = newPlatform.Description;

            bookContext.Platforms.InsertOnSubmit(platform);
            bookContext.SubmitChanges();

            return platform.ID;
        }
        catch
        {
            return 0;
        }
    }

    /// <summary>
    ///Update Platform 
    /// </summary>
    /// <returns></returns> 
    public long UpdatePlatform(PlatformEntity newPlatform)
    {
        BookContextDataContext context = new BookContextDataContext();
        try
        {
            Platform platform = context.Platforms.SingleOrDefault(p => p.ID == newPlatform.ID);
            if (platform != null)
            {

                platform.Name = newPlatform.Name;
                platform.Description = newPlatform.Description;
                context.SubmitChanges();

                return platform.ID;
            }

        }
        catch
        {
        }
        return 0;
    }

    /// <summary>
    /// Delete Platform by platformID
    /// </summary>
    /// <param name="platformID"></param>
    /// <returns></returns> 
    public bool DeletePlatform(long platformID)
    {
        BookContextDataContext context = new BookContextDataContext();
        try
        {
            Platform platform = context.Platforms.SingleOrDefault(p => p.ID == platformID);
            context.Platforms.DeleteOnSubmit(platform);
            context.SubmitChanges();

            return true;
        }
        catch { }
        return false;
    }
}